/**
 * 删除从属表
 * @param {} params 
 */
async function atom(params) {
	console.log(params);
	const spaceid = params.spaceid;
	const systemid = params.systemid;
	const table = params.table;
	const data = params.data;
	const del_data_argjson = []; // 所有要删除的数据
	const find_del_data_param = []; // 所有要查询的数据
	data.forEach(async (item, i) => {
		console.log(i, item);
		del_data_argjson.push({ tname: table.tablename, opflag: 2, criteria: item });// 先组织待删除的主表的数据，
		recursion_table(table); //再根据关联字段查询关联表并组织待删除的关联数据
		console.log(del_data_argjson);
		// const del_res = await del_data(spaceid, systemid, del_data_argjson); // 调用删除服务,删除所有数据
		// console.log(del_res);
		return del_res;
	});
	async function recursion_table(table) {// 递归删除从属表
		const tablename = table.tablename;
		const ref_tablename = table.ref_tablename;
		const ref_fieldname = table.ref_fieldname;
		ref_tablename.forEach((tname) => {
			const criteria = {};
			criteria[ref_fieldname] = tname[ref_fieldname];
			console.log(criteria);
			if (typeof tname == "string") {
				find_del_data_param.push({
					"query_mode": 1, "spaceid": spaceid, "systemid": systemid, "tablename": tname, "criteria": criteria,
					"fields": { "_id": 1 }, "page_no": 1, "page_size": 0, "sort": {}, "sql": ""
				}); // 组织查询关联表参数
			} else {
				recursion_table(tname);
			}
		});
		const ref_find_res = await find_del_data(find_del_data_param); // 查询所有关联表数据
		ref_find_res.forEach((tref) => {
			del_data_argjson.push({ tname: tname, opflag: 2, criteria: tref });
		});
	}
	async function find_del_data(query) {
		const result = await send_msg("dbacc.find.intranet", query);
		const res = _docode(result);
		return res;
	}
	function _docode(views) { // 解析得到查询的表数据
		for (let i = 0; i < views.length; i++) {
			const view = views[i];
			if (view["tablename"] == tablename) {
				const data_array = view["dataArray"];
				return data_array;
			}
		};
		return [];
	}

	async function del_data(spaceid, systemid, argjson) {
		let out_put = {
			spaceid: spaceid,
			systemid: systemid,
			argjson: argjson
		}
		const res = await send_msg("dbacc.save.intranet", out_put);
		return res;
	}
}


//渠道设备channel_equipment
//设备详细参数equipment_details
//设备容量信息equipment_capacity
//设备在线状态equipment_online_state
//屏幕screen
//设备启动记录equipment_startup_record
//屏幕截图screen_screenshot
const p = {
	spaceid: "xmedia", systemid: "1093",
	table: {
		tablename: "channel_equipment",
		ref_fieldname: "equip_no",
		ref_tablename: [
			"equipment_details",
			"equipment_capacity",
			"equipment_online_state",
			"equipment_startup_record",
			{
				tablename: "screen",
				ref_tablename: "screen_screenshot",
				ref_fieldname: "screen_no"
			}
		]
	},
	data: [{ _id: "sss", nequip_no: "equip_no1" }, { _id: "ssds", equip_no: "equip_no2" }]
};
const code = atom.toString();
if (require.main === module) {
	const iv = require('../dist/invoke');
	iv.default(code, p, 'test', 'test', 'xmedia').then((rc) => {
		console.log(JSON.stringify(rc));
	}, (err) => {
		console.error(err);
	});
}